﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using WF_Business;
using ExtBusiness.FlowOperation;
using ExtBusiness.Common;

public partial class UserControls_FileUpload : System.Web.UI.UserControl
{
        //流程ID 
        public string iid
        {
            get
            {
                return ViewState["iid"] == null ? "-1" : ViewState["iid"].ToString();
            }
            set
            {
                ViewState["iid"] = value;
            }
        }
        //岗位ID
        public string wiid
        {
            get
            {
                return ViewState["wiid"] == null ? "-1" : ViewState["wiid"].ToString();
            }
            set
            {
                ViewState["wiid"] = value;
            }
        }

        //岗位ID
        public string ctlid
        {
            get
            {
                return ViewState["ctlid"] == null ? "-1" : ViewState["ctlid"].ToString();
            }
            set
            {
                ViewState["ctlid"] = value;
            }
        }

        //岗位名称
        public string step
        {
            get
            {
                string strReturn = string.Empty;

                if (!string.IsNullOrEmpty(wiid))
                {
                    ClsWorkFlow cwfTemp = new ClsWorkFlow();
                    strReturn = cwfTemp.GetStepByWiid(wiid);
                }
                return strReturn;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //判断附件权限
                AttachmentFileUpload attachFile = new AttachmentFileUpload();
                int Flag = attachFile.GetAttachmentRight(iid, step);

                if (Flag == 0)
                {
                    Flagdiv.Visible = false;
                    Flagdiv1.Visible = true;
                }
            }
        }

        /// <summary>
        /// 获取目录
        /// </summary>
        /// <returns></returns>
        public String GetTreeHTML()
        {
            WorkFlowResource wfr = new WorkFlowResource();
            DataTable dt = new DataTable();
            if ("-1".Equals(wiid))
            { dt = wfr.ListInstanceResource(iid); }
            else
            { dt = wfr.ListInstanceStepResource(wiid.ToString(), ctlid, iid.ToString()); }
            dt.Columns.Add(new DataColumn("ParentId"));
            dt.Columns.Add(new DataColumn("Site"));
            dt.Columns.Add(new DataColumn("Name"));
            DataView dv = dt.DefaultView;
            dv.Sort = "Path";
            for (int i = 0; i < dv.Count; i++)
            {
                string path = dv[i]["Path"].ToString();
                string[] tmps = path.Split('/');
                dv[i]["Site"] = i + 1;
                if (tmps.Length > 1)//不是顶节点
                {
                    dv[i]["Name"] = tmps[tmps.Length - 1];
                    string pre = path.Substring(0, path.LastIndexOf('/'));
                    DataRow[] drs = dt.Select("Path='" + pre + "'");
                    if (drs.Length > 0)
                        dv[i]["ParentId"] = drs[0]["Site"].ToString();
                }
                else
                {
                    dv[i]["Name"] = dv[i]["Path"].ToString();
                    dv[i]["ParentId"] = 0;
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='path' id='myfilePath1' class='select'>\n");
            sb.Append("<option value='/'>/</option>\n");
            if ("-1".Equals(wiid))
            { printSubNodes(dt, sb, ""); }
            else
            {
                string wid = WorkFlowResource.GetWidByIID(iid);
                printSubNodes(dt, sb, wid);
            }
            sb.Append("</select>\n");
            return sb.ToString();
        }
        /// <summary>
        /// 查找子节点
        /// </summary>
        /// <param name="dt">树形结构数据集</param>
        /// <param name="sb">返回字符串</param>
        /// <param name="wid">岗位ID</param>
        private void printSubNodes(DataTable dt, StringBuilder sb, string wid)
        {
            foreach (DataRow dr in dt.Rows)
            {
                string type = dr["type"].ToString();
                String strOutput = string.Empty;
                DataRow[] drs = dt.Select("ParentId='" + dr["Site"].ToString() + "'");
                if (drs.Length > 0)//有子节点，说明不是叶结点
                {
                    strOutput = "<option value=\"" + dr["Path"].ToString() + "\">" + dr["Name"].ToString() + "</option>";
                }
                else
                {
                    if (type == "0")//目录
                    {
                        strOutput = "<option value=\"" + dr["Path"].ToString() + "\">" + dr["Name"].ToString() + "</option>";
                    }
                }
                sb.Append(strOutput);
            }
        }
    }